/**
 * Created by steve on 14-9-18.
 */

// 配置
require.config({
    paths: {
        jquery: '../libs/jquery-1.11.1/jquery-1.11.1.min',
        jquerymobile: '../libs/jquery.mobile-1.4.4/jquery.mobile-1.4.4.min',
        net: './net',
        md5: './md5',
        logon: './logon',
        newsroom: './newsroom',
        active:'./active',
        forget_password: './forget_password', 
        phonegap: '../phonegap',
        dialogs:'./dialogs',
        filter:'./filter',
        setting:'./setting',
        activity_register:'./activity_register', 
        activity_calendar_info:'./activity_calendar_info', 
        activity_verify:'./activity_verify',
        activity_confirm:'./activity_confirm',
        surveyConfirm:'./survey_confirm',
		voting:'./voting', 
        videoDetails:"./videoDetails",  
        mycorner:'./mycorner',
        mycornerall:'./mycornerall',
        uploadInfo:'./upload_info',
        book:'./book',
        bookrule:'./book_rule',
        bookDetailed:'./book_detail',
        bookrequest:'./book_request',
        bookconfirm:'./book_confirm',
        bookcheck:'./book_check',
        bookRecordDetail:'./bookrecord_detail',
        questionnaires:'./questionnaires',
        surveyQuestionnaires:'./survey_questionnaires',
        panle:'./panle',
        ca:'./ca',
        addRegistration:'./add_registration',
        registration:'./registration_confirm',
        registrationShow:'./registration_show',
        registrationRecord:'./registration_record',
        registrationTerm:'./registration_term',
        registrationRule:'./registration_rule',
		teamRegistration:'./team_registration',
		registrationManager:'./registration_manager',
		swiper:'../libs/Swiper-3.4.2.0/js/swiper.min',
		canteen:'./canteen',
		canteenrule:'./canteen_rule',
		canteensuper:'./canteen_supplier'
    }
});

require(['logon', 'newsroom', 'active', 'forget_password', 'filter', 'setting', 'infomation', 'question', 'policy',
    'activity_calendar_info', 'activity_register', 'activity_verify', 'activity_confirm','voting','videoDetailed',
    'mycorner','mycornerall','book','bookrule','bookDetailed','bookrequest','bookconfirm','bookcheck','bookRecordDetail','questionnaires',
    'surveyConfirm','surveyQuestionnaires','uploadInfo','ca','add_registration','registration_confirm','registration_show','registration_record','registration_term','registration_rule','team_registration','registration_manager','canteen','canteenrule','canteensuper','panle'], function(phonegap) {
    // 全局变量开启
    window.q = {};
	// Fix menu scrolling issue
	$("body").on("touchmove", ".ui-panel-open, .ui-panel-dismiss-open", function(e) {
		e.preventDefault();
	});
	
	//Bind keypress chars limited control event for all username and password input field
	$("#active_txt_username,#active_pass_password,#change_password-txt-currentPassword,#change_password-txt-newPassword,#change_password-txt-newPassword-confirm,#logon_txt_username,#logon_txt_password").on("keypress",function(event){
		if(!event){
			event = window.event;
		}
		if(event.currentTarget.value.length+1>15){
			if(event.keyCode != 8){
				event.returnValue = false;
				return false;
			}
		}else{
			return true;
		}
	});
	
	window.scrollForAndroid = function(contentId){
		var androidScroll = null;
		if(window.navigator.userAgent.substr(window.navigator.userAgent.indexOf('Android') + 8, 3) < 4.0){
			androidScroll = new iScroll(contentId);
		}
		return androidScroll;
	}
	
	window.setBodyOverflow = function(body){
		if(window.navigator.userAgent.indexOf('Android')!= -1 && window.navigator.userAgent.substr(window.navigator.userAgent.indexOf('Android') + 8, 3) < 4.0){
			body.css('overflow-y','hidden !important');
		}else{
			body.css('overflow-y','hidden');
		}
	}
	
	window.historyView = [];
	$.mobile.newChangePage = function(to, options){
		historyView.unshift('#'+$('.ui-page-active').attr('id'));
//      if(historyView.inArray('#'+$('.ui-page-active').attr('id')) == false){
//		    historyView.unshift('#'+$('.ui-page-active').attr('id'));
//      }
        if(to == null){
            window.shouldPageRefresh.newsroom = true; 
            to = "#newsroom";
        }
        if(linkdetailList.length > 0){
            if(to == "#newsroom"){
                linkdetailList = [];
                $("#video_detailed").removeClass("hrefback"); 
            }
        }
		$.mobile.changePage(to, options);
	}
    function inArray(value,array){
        for(var i in array){
            if(value===array[i]){
                return true;
            }
        }
        return false;
    }
	changeMenuBackFromGreenLaiSee = function( id ){
		id = id.replace('push_','#');
		if(id == '#AR'){
			cordova.exec(function(){console.error("success")}, function(){console.error("error")}, "ShowVideoPlaybackView", "showVideoPlaybackView", []);
		}else {
			if(id == '#logon') chrome.storage.internal.remove('password', function(){});
			$.mobile.newChangePage(id,{ transition: "slide",reverse: false,changeHash: false});
		}
	}
	
	//Compatible with render code version  
	$.mobile.backChangePage = function(to, options){
        console.error(historyView.join(",")+"@@@@backChangePage@@@@"+to);
		historyView.shift();
        console.error(historyView.join(",")+"!!!!backChangePage");
        if(to == null){
            window.shouldPageRefresh.newsroom = true; 
            to = "#newsroom";
        }
		$.mobile.changePage(to, { transition: "slide",reverse: true,changeHash: false});
	}

	$.mobile.goBackPage = function(){
        console.error(historyView.join(",")+"@@@@goBackPage");
		$.mobile.changePage(historyView.shift(), { transition: "slide",reverse: true,changeHash: false});
	}
	
	document.addEventListener("backbutton", function(e){
		if($('#errorAlert').length <= 0){
			if(window.historyView.length <= 0){
                if($(".panle_btn_menu").hasClass('ui-panel-closed')){
                    $('.panle_btn_menu').click();
                }else{
                    //exitapp
                }
			}else $.mobile.changePage(window.historyView.shift(), { transition: "slide",reverse: true,changeHash: false});
		}
	});
	// Nick added for pull to refresh start
	window.shouldPageRefresh = {
		newsroom: true, 
        book:true
    };

	window.getVendorStyle = function(style) {
		var dummyStyle = document.createElement("div").style,
		    vendor = (function() {
				var vendors = "t,webkitT,MozT,msT,OT".split(","),
					t,
					i = 0,
					l = vendors.length;

				for (; i < l; i++) {
					t = vendors[i] + "ransform";
					if (t in dummyStyle) {
						return vendors[i].substr(0, vendors[i].length - 1);
					}
				}

				return false;
			})();
		vendor ? "-" + vendor.toLowerCase() + "-" : "";

		if (vendor === "") return style;

		style = style.charAt(0).toUpperCase() + style.substr(1);
		return vendor + style;
	};
	// Nick added for pull to refresh end
	// Nick added for multiple images start 
    window.linkdetailList = []; 
	// Nick added for multiple images end
});